package net.helix.fileChooser;

import java.io.File;

import javax.swing.filechooser.FileFilter;

/**
 * Sets the filter for use in file choosers which can take a list of allowed file types as a string array.
 * @author Runouw
 *
 */
public class ImageFilter extends FileFilter {
	String[] fileType = new String[1];
	String fileTypeName = "";
	public ImageFilter(String fileType, String fileTypeName){
		this.fileType[0] = fileType;
		this.fileTypeName = fileTypeName;
	}
	public ImageFilter(String[] fileType, String fileTypeName){
		this.fileType = fileType;
		this.fileTypeName = fileTypeName;
	}
	@Override
	public boolean accept(File f) {
		if(f.isDirectory()) return true;
		String ext = getExtension(f.getName());
		for(int i=0;i<fileType.length;i++){
			if(ext.equalsIgnoreCase(fileType[i])) return true;	
		}
		return false;
	}

	@Override
	public String getDescription() {
		return fileTypeName;
	}
	
	public static String getExtension(String name){
		int dot = name.lastIndexOf('.');
		if(dot != -1){
			return name.substring(dot+1);
		}
		return "";
	}
}
